<?=$this->renderPartial("tabNavigation", array('s_tabSelected'=>'index'));?>

<div class="clear"></div>

<div class="box">
    <div class="box_content" id="div_contentBox">
        <div class="button_navigation">
            <button 
                id      = "btn_refresh"
                class   = "button_round"
                onclick = "load_page('<?=Yii::app()->createUrl("category/index");?>');"
            >
                <i class="fa fa-refresh"></i>
            </button>

            <button id="btn_delete" class="button_round">
                <i class="fa fa-trash-o"></i>
            </button>

            <button id="btn_excelExport" class="button_round">
                <i class="fa fa-download"></i>
            </button>
        </div><!--End button_navigation-->

        <table class="tb_general" id="tb_category">
            <tr>
                <th class="text_center" style="width:2%;">
                    <span class="fa fa-trash-o fa-2x"></span>
                </th>

                <th style="width: 2%;">STT</th>
                <th onclick="sortData('<?=Category::model()->getAttributeLabel('name');?>', this);"   style="cursor: pointer;"><i class="fa fa-sort">&nbsp;</i>Danh Mục</th>
                <th onclick="sortData('<?=Category::model()->getAttributeLabel('image');?>', this);"  style="cursor: pointer;"><i class="fa fa-sort">&nbsp;</i>Hình Ảnh</th>
                <th onclick="sortData('<?=Category::model()->getAttributeLabel('parent');?>', this);" style="cursor: pointer;"><i class="fa fa-sort">&nbsp;</i>Danh Mục Cha</th>
                <th onclick="sortData('<?=Category::model()->getAttributeLabel('insert');?>', this);" style="cursor: pointer;"><i class="fa fa-sort">&nbsp;</i>Ngày Nhập</th>
            </tr>

            <tr id="tr_search">
                <td>
                    <input
                        type    = "checkbox"
                        class   = "check_box"
                        onclick = "setGeneralStatusOfCheckboxes(this, 'chk_delete')"
                    />
                </td>

                <td><i class="fa fa-search fa-2x"></i></td>

                <td colspan="7">
                    <form
                        id     = "frm_search"
                        name   = "frm_search"
                        method = "get"
                        action = "<?=Yii::app()->createUrl("category/index");?>"
                    >

                    <input
                        type  = "hidden"
                        id    = "hid_columnName"
                        name  = "hid_columnName"
                        value = "<?=$s_columnLabel;?>"
                    />

                    <input
                        type  = "hidden"
                        id    = "hid_columnPosition"
                        name  = "hid_columnPosition"
                        value = "<?=$i_columnPosition;?>"
                    />

                    <input
                        type  = "hidden"
                        id    = "hid_sortMethod"
                        name  = "hid_sortMethod"
                        value = "<?=$s_sortMethod;?>"
                    />

                    <input
                        type     = "text"
                        id       = "txt_filter"
                        name     = "txt_filter"
                        class    = "text_box"
                        value    = "<?=$s_filter;?>"
                        tabindex = "1"
                    />
                    </form>
                </td>
            </tr>

            <tbody id="tbody_category">
                <?php
                $i_stt = 0;
                
                foreach ( $arr_data as $key => $o_category)
                {
                    $date_insert = new DateTime($o_category->insert);
                    $i_stt +=1;
                ?>
                <tr id="tr_<?=$o_category->identification;?>">
                    <td>
                        <input
                            name  = "chk_delete"
                            type  = "checkbox"
                            class = "check_box"
                            value = "<?=$o_category->identification;?>"
                        />
                    </td>

                    <td><?=$i_stt;?></td>

                    <td>
                        <a
                           href    = "javaScript:void(0);"
                           onclick = "call_edit_page(<?=$o_category->identification;?>)"
                        >
                            <i class="fa fa-edit fa-2x"></i>
                            <?=$o_category->name;?>
                        </a>
                    </td>

                    <td><?=$o_category->image;?></td>
                    <td><?=CHtml::decode($o_category->parent_name);?></td>
                    <td><?=$date_insert->format("Y-m-d");?></td>
                </tr>
                <?php }?>

                <tr>
                    <td colspan="10">
                        <?php
                        $this->widget(
                            "PagingWidget",
                            array (
                                "totalItem"     => $i_totalItem,
                                "amountPerPage" => $i_amountPerPage,
                                "currentPage"   => $i_currentPage
                            )
                        );
                        ?>
                        <div class="clear"></div>
                    </td>
                </tr>
            </tbody>
        </table><!--End tb_general-->

        <div class="clear"></div>
    </div><!--End box_content-->
</div><!--End box-->

<div
    id    = "dialog_excute"
    title = "Chương trình đang thực thi"
    class = "dialog_message"
>
    <p class="center">
        <img alt="Loading...." src="<?=Yii::app()->baseUrl;?>/images/image_default/loading_circle.gif" width="92px" height="92px"/>
    </p>
</div><!--End dialog_excute-->

<div
    id    = "dialog_notice"
    title = "Người Dùng"
    class = "dialog_message"
>
    <div id="content">
    </div>
</div><!--End dialog_notice-->

<script type="text/javascript">

$(document).ready(function ()
{
    $("th").each(function (index, element)
    {
        if ( index == <?=$i_columnPosition;?> ) {
            //remove default symbol of sort method
            $(element).children("i").first().removeClass("fa-sort");

            //Set symbol to show current sort method
            if ( $("#hid_sortMethod").val() == "DESC" ) {
                $(element).children("i").addClass("fa-sort-desc");
            } else if ($("#hid_sortMethod").val() == "ASC") {
                $(element).children("i").addClass("fa-sort-asc");
            }
        }//End if
    });//End $("th").each

    $("#dialog_excute").dialog({
        autoOpen      : false,
        closeOnEscape : false,
        width         : 220,
        height        : 152,
        modal         : true,
        resizable     : false,
        show          : "explode",
        hide          : "explode",
        open          : function (event, ui) { $(".ui-dialog-titlebar-close").hide();}
    });

    $("#dialog_notice").dialog({
        autoOpen      : false,
        closeOnEscape : true,
        width         : "99.16666666%",
        height        : "auto",
        modal         : true,
        show          : "explode",
        hide          : "explode",
        open          : function (event, ui) {$(".ui-dialog-content").css("padding", "0px");}
    });
});//End $(document).ready


function sortData (columnName, element)
{
    var s_sort          = $("#hid_sortMethod").val();
    var s_currentColumn = $("#hid_columnName").val();

    $("#hid_columnName").val(columnName);
    $("#hid_columnPosition").val($("th").index(element));

    if ( s_currentColumn == columnName && s_sort == "DESC" ) {
        $("#hid_sortMethod").val("ASC");
    } else if ( s_currentColumn == columnName && s_sort == "ASC" ) {
        $("#hid_sortMethod").val("");
        $("#hid_columnName").val("");
        $("#hid_columnPosition").val("");
    } else {
        $("#hid_sortMethod").val("DESC");
    }

    $("#frm_search").submit();
}//End sortData


function call_edit_page(i_key) {
    $("#tr_"+i_key).css("background-color","#EFEFEF");
    $("#dialog_excute").dialog( "open" );

    $.ajax({
        url     : "<?=Yii::app()->createUrl("category/edit");?>",
        type    : "POST",
        cache   : false,
        data    : {i_key : i_key},
        success : function(html)
        {
            $("#dialog_notice").children("#content").html(html);
            $("#dialog_excute").dialog( "close" );
            $("#dialog_notice").dialog("open");
        },
        error: function ()
        {
            alert("Trang hiện hành không tồn tại.");
            $("#dialog_excute").dialog( "close" );
        }
    });//End ajax
}//End call_edit_page


$('#btn_delete').click(function() 
{
    if(!confirm("Bạn muốn xóa các đối tượng đã chọn?")) {
        return;
    }

    $("#dialog_excute").dialog( "open" );

    $.ajax({
        url      : "<?=Yii::app()->createUrl("category/delete");?>",
        type     : "POST",
        cache    : false,
        data     : { arr_deleteId : $("input[name='chk_delete']:checked").serializeArray() },
        dataType : "html",
        success  : function(string) 
        {
            var arr_result = $.parseJSON(string);

            if(arr_result.error != null)
            {
                alert(arr_result.error);
                $("#dialog_excute").dialog( "close" );
                return;
            }//End if

            var arr_deleteId = $("input[name='chk_delete']:checked").serializeArray();

            for ( var int = 0; int < arr_deleteId.length; int++)
            {
                $("#tr_"+ arr_deleteId[int].value).remove();
            }//End for

            $("#dialog_excute").dialog( "close" );
        },//End success
        error: function ()
        {
            alert("Trang hiện hành không tồn tại.");
            $("#dialog_excute").dialog( "close" );
        }//End error
    });//End ajax
});//End $('#btn_delete, #span_delete').click
</script>